package com.cloud.drore.eboos.common.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

import static org.springframework.boot.autoconfigure.klock.model.LockType.Fair;


/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/3/23 0023 上午 11:18 <br>
 * @Author: <a href="410508961@qq.com">夏凯</a>
 */
@Component
public class RedisKeysClockUtil {

    @Resource
    JedisPoll jedisPoll;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @param key
     * @param money
     * @param flag
     * @ClassName: RedisKeysClockUtil
     * @Return: java.math.BigDecimal
     * @Decription: 根据key值，金额变动值，变动类型得到变动之后的资金池金额
     * @CreateDate: Created in 2018/3/23 0023 下午 2:32
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Klock(keys = {"{#key}"}, lockType = Fair, waitTime = 10)
    //true 加  false 减
    public BigDecimal cashPoolChange(String key, BigDecimal money, Boolean flag) {
        String beginMoney = stringRedisTemplate.opsForValue().get(key).toString();
        if (flag) {
            //退款，充值
            BigDecimal endMoney = new BigDecimal(beginMoney).add(money);
            stringRedisTemplate.opsForValue().set(key, endMoney.toString());
            return endMoney;
        } else {
            //下单
            BigDecimal endMoney = new BigDecimal(beginMoney).subtract(money);
            stringRedisTemplate.opsForValue().set(key, endMoney.toString());
            return endMoney;
        }
    }

    /**
     * @param key
     * @param count
     * @param flag
     * @ClassName: RedisKeysClockUtil
     * @Return: java.lang.Integer
     * @Decription: 根据key值，库存变动值，变动类型得到变动之后的库存
     * @CreateDate: Created in 2018/3/23 0023 下午 2:35
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Klock(keys = {"{#key}"}, lockType = Fair, waitTime = 10)
    public Integer stockChange(String key, String date, Integer count, Boolean flag) {
        //true 加  false 减
        String beginCount = stringRedisTemplate.opsForHash().get(key,date).toString();
        if (flag) {
            // 退订
            Integer endCount = Integer.parseInt(beginCount) + count;
            stringRedisTemplate.opsForHash().put(key,date,endCount.toString());
            return Integer.parseInt(beginCount) + count;
        } else {
            //下单
            Integer endCount = Integer.parseInt(beginCount) - count;
            stringRedisTemplate.opsForHash().put(key,date,endCount.toString());
            return endCount;
        }
    }


    /**
     * @ClassName: RedisKeysClockUtil
     * @param key
     * @Return: java.lang.String
     * @Decription: 用于拒绝多次重复请求
     * @CreateDate: Created in 2018/4/12 19:03
     * @Author: <a href="747639122@qq.com">冯腾</a>
     * @Modify:
     */
    @Klock(lockType = Fair,waitTime = 2)
    public boolean clockVersion(String key){
        // 判断redis中是否已有此key
        if (!stringRedisTemplate.hasKey(key)){
            stringRedisTemplate.opsForValue().set(key, "true");
            return true;
        }
        return false;

    }

}
